package com.lvyq.javaprint.view.index;

import com.lvyq.javaprint.constant.TrayConstant;
import com.lvyq.javaprint.controller.FXProxySettingController;
import com.lvyq.javaprint.view.style.STYLE;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.StringConverter;
import java.util.HashMap;
import java.util.Map;


/**
 * @author lvyq 代理设置
 * @version 1.0
 * Create by 2025-01-06 17:11
 */
public class ProxySettingView {


    //代理主页面
    static BorderPane proxyView = new BorderPane();
    static FXProxySettingController proxyController;

    static  ComboBox<String> comboBox;

    /**
     * @description 创建代理设置内容
     * @author lvyq
     * @throws
     * @return VBox
     * @time 2025-01-07 16:33
     */
    public static BorderPane createProxyBoxView( ScrollPane scrollPane,VBox printView ) {

        //代理界面最外层
        proxyView.setPrefWidth(TrayConstant.stageW-TrayConstant.pane_Hor_Margin*2-3.50);
        proxyView.setPrefHeight(TrayConstant.stageH-TrayConstant.pane_Vet_Margin*2-28.00);
        proxyView.setStyle("-fx-background-color: rgba(0.0,0.0,0.0,0.0);-fx-border-color: rgba(0.0,0.0,0.0,0.2);" +
                "-fx-border-width: 2;-fx-border-radius: 4");


        // ip地址
        TextField domainField = new TextField();
        // 端口号
        TextField portField = new TextField();

        comboBox= createProxyTypeView();

        proxyController= new FXProxySettingController(comboBox,domainField,portField);
        proxyController.loadData();

        HBox centerBox = new HBox(10);
        centerBox.setPrefHeight(95.00);
        //宽度小于scrollPane宽度，避免出现水平滑动框
        centerBox.setPrefWidth(TrayConstant.stageW-TrayConstant.pane_Hor_Margin*2-10.00);
        centerBox.setStyle("-fx-background-color: rgba(0.0,0.0,0.0,0.0);  -fx-background-insets: 0;");

        VBox domainBox=  new VBox();
        domainBox.setPrefWidth(TrayConstant.stageW-TrayConstant.pane_Hor_Margin*2-10.00);
        domainBox.setStyle("-fx-background-color: rgba(0.0,0.0,0.0,0.0);  -fx-background-insets: 0;");
        Label domainLabel = new Label("IP地址") ;

        domainField.setPromptText("请输入IP地址"); // 设置提示文本
        domainBox.getChildren().addAll(domainLabel,domainField);

        VBox portBox = new VBox();
        portBox.setPrefWidth(TrayConstant.stageW-TrayConstant.pane_Hor_Margin*2-10.00);
        portBox.setStyle("-fx-background-color: rgba(0.0,0.0,0.0,0.0);  -fx-background-insets: 0;");
        Label portLabel = new Label("端口");

        portField.setPromptText("请输入端口号"); // 设置提示文本
        //portField.setText(proxyPort);
        portBox.getChildren().addAll(portLabel,portField);
        centerBox.getChildren().addAll(domainBox,portBox);

        //不启用代理
        HBox centerBox2 = new HBox(5);
        centerBox2.setPrefHeight(95.00);
        centerBox2.setPrefWidth(TrayConstant.stageW-TrayConstant.pane_Hor_Margin*2-10.00);
        centerBox2.setStyle("-fx-background-color:rgba(0.0,0.0,0.0,0.0);  -fx-background-insets: 0;");
        centerBox2.getChildren().add(new Label(""));
        ScrollPane proxyPane;
        //初始化
        if (comboBox.valueProperty().getValue().equals("1")){
            proxyPane = new ScrollPane(centerBox2);
        }else {
            proxyPane = new ScrollPane(centerBox);
        }

        proxyView.setCenter(proxyPane);
        comboBox.valueProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
                //切换内容
                Node currentContent = proxyPane.getContent();
                if (currentContent == centerBox ) {
                    proxyPane.setContent(centerBox2);
                } else {
                    proxyPane.setContent(centerBox);
                }
            }
        });




        HBox bottomBox = new HBox(5);
        bottomBox.setAlignment(Pos.TOP_RIGHT);
        //宽度小于scrollPane宽度，避免出现水平滑动框
        bottomBox.setPrefWidth(TrayConstant.stageW-TrayConstant.pane_Hor_Margin*2-10.00);
        bottomBox.setPrefHeight(30.00);
        Button saveBut = new Button("确定");
        STYLE.BUTTON.info_style(saveBut);
        Button closeBut =  new Button("取消");
        STYLE.BUTTON.tertiary_style(closeBut);
        bottomBox.getChildren().addAll(saveBut,closeBut);
        proxyView.setBottom(bottomBox);


        //跳转至代理设置页面
        saveBut.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                // 切换ScrollPane的内容
                //更新设置
                proxyController.updateSetting();
                Node currentContent = scrollPane.getContent();
                if (currentContent == printView) {
                    scrollPane.setContent(proxyView);
                } else {
                    scrollPane.setContent(printView);
                }
            }
        });

        closeBut.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                // 切换ScrollPane的内容
                Node currentContent = scrollPane.getContent();
                if (currentContent == printView) {
                    scrollPane.setContent(proxyView);
                } else {
                    scrollPane.setContent(printView);
                }
            }
        });

        return proxyView;
    }




    /**
     * @description 代理类型视图
     * @author lvyq
     * @throws
     * @time 2025-01-09 15:59
     */
    public static  ComboBox createProxyTypeView(){
        comboBox = new ComboBox<>();
        VBox topBox = new VBox();
        //宽度小于scrollPane宽度，避免出现水平滑动框
        topBox.setPrefWidth(TrayConstant.stageW-TrayConstant.pane_Hor_Margin*2-10.00);
        Label typeLabel = new Label("类型");
        Map<String,String> dataMap = new HashMap<>();
        dataMap.put("1","不使用代理");
        dataMap.put("2","网页代理(HTTP)");
        comboBox.getItems().addAll(dataMap.keySet());

        // 自定义显示值
        comboBox.setConverter(new StringConverter<String>() {
            @Override
            public String toString(String key) {
                return dataMap.get(key);
            }
            @Override
            public String fromString(String s) {
                return null;
            }
        });

        // 自定义下拉列表中的单元格显示
        comboBox.setCellFactory(lv -> new ListCell<String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if (empty || item == null) {
                    setText(null);
                    setGraphic(null);
                } else {
                    setText(dataMap.get(item));
                }
            }
        });
        topBox.getChildren().addAll(typeLabel,comboBox);
        proxyView.setTop(topBox);
        return comboBox;
    }

    /**
     * @description 创建代理点击事件
     * @author lvyq
     * @param[1] proxyBut
     * @param[2] scrollPane
     * @param[3] proxyPane
     * @param[4] printView
     * @throws


     * @time 2025-01-09 14:29
     */
   public static void proxyButFunction(Button proxyBut, ScrollPane scrollPane, BorderPane proxyView, VBox printView,Button viewTitle){
       proxyBut.setOnMouseClicked(new EventHandler<MouseEvent>() {
           @Override
           public void handle(MouseEvent event) {
               // 切换内容
               Node currentContent = scrollPane.getContent();
               if (currentContent == printView ) {
                   viewTitle.setText("代理设置");
                   //重现加载页面
                   createProxyBoxView(scrollPane,printView);
                   //切换
                   scrollPane.setContent(proxyView);
               } else {
                   viewTitle.setText("打印服务列表");
                   scrollPane.setContent(printView);
               }
           }
       });

   }
}
